package com.smart.mip.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smart.mip.system.common.BaseController;
import com.smart.mip.system.common.JsonResultUtils;
import com.smart.mip.system.common.PageDesc;
import com.smart.mip.system.common.ResponseData;
import com.smart.mip.system.dao.UserInfoRepository;
import com.smart.mip.system.entity.UserInfo;
import com.smart.mip.system.service.UserInfoService;
/**
 * UserInfo  Controller.
 * create by scaffold 2017-07-18 
 * @author codefan@sina.com
 * 系统用户信息null   
*/


@RestController
@RequestMapping("/system/userinfo")
public class UserInfoController extends BaseController{
	
	@Autowired
	private UserInfoRepository userInfoRepository;	

	@Autowired
	private UserInfoService userInfoService;
	
    /**
     * 查询所有   系统用户信息  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
	@RequestMapping(value = "list", method=RequestMethod.POST)
    public void list(HttpServletRequest request, PageDesc pageDesc, HttpServletResponse response) {   
		Page<UserInfo> listObjects = userInfoService.findUserInfoNoCriteria(pageDesc.getPageNo() - 1, pageDesc.getPageSize());
		JSONArray array = JSON.parseArray(JSON.toJSONString(listObjects.getContent()));
		JSONObject obj = new JSONObject();
		obj.put("total", listObjects.getTotalElements());
		obj.put("rows", array);
		JsonResultUtils.writeOriginalJson(obj, response);
    }
    
    /**
     * 查询单个  系统用户信息 
	
	 * @param userCode  USER_CODE
     * @param catalogCode 主键
     * 
     * @param response    {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{userCode}", method = {RequestMethod.GET})
    public void getUserInfo(@PathVariable String userCode, HttpServletResponse response, Model model) {
    	
    	UserInfo userInfo =     			
    			userInfoRepository.findOne( userCode);
        
        model.addAttribute(userInfo);
    }
    
    /**
     * 新增 系统用户信息
     *
     * @param userInfo  {@link UserInfo}
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST})
    public String createUserInfo(@RequestBody @Valid UserInfo userInfo, HttpServletResponse response) {
    	userInfoRepository.save(userInfo);
        return "redirect:/";
    }

    /**
     * 删除单个  系统用户信息 
	
	 * @param userCode  USER_CODE
     */
    @RequestMapping(value = "/{userCode}", method = {RequestMethod.DELETE})
    public String deleteUserInfo(@PathVariable String userCode, HttpServletResponse response) {
    	
    	userInfoRepository.delete( userCode);
        
       return "redirect:/";
    } 
    
    /**
     * 新增或保存 系统用户信息 
    
	 * @param userCode  USER_CODE
	 * @param userInfo  {@link UserInfo}
     * @param response    {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{userCode}", method = {RequestMethod.PUT})
    public String updateUserInfo(@PathVariable String userCode, 
    	@RequestBody @Valid UserInfo userInfo, HttpServletResponse response) {
    	
    	
    	UserInfo dbUserInfo  =     			
    			userInfoRepository.findOne( userCode);
        
        

        if (null != userInfo) {
        	dbUserInfo .copy(userInfo);
        	//userInfoRepository.saveAndFlush(dbUserInfo);
        } 

        return "redirect:/";
    }
}
